package system.wrap_filter;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.dbcp.BasicDataSource;

import com.hong.alert.dao.ReferenceDataRepossitory;

import models.SubscribeEvents;
import system.eventBuffer.EventBuffer;
import system.main.SystemDataStructure;

public class Scheduler extends Thread {

	private ReferenceDataRepossitory referenceRepository;
	BasicDataSource dataSource;
	SystemDataStructure ds;
	ExecutorService executor;
	Connection connection;
	Statement statement;
	ResultSet resultSet;
	SubscribeEvents subscribeEvents;
	EventBuffer eventBuffer;
	ArrayList<Policy> policies;
	boolean run;
	int i = 1;

	public Scheduler(SystemDataStructure ds) {
		this.ds = ds;
		this.run = true;
		eventBuffer = ds.getEventBuffer();
		policies = new ArrayList<Policy>(7);
		referenceRepository = ds.getReferenceDataRepossitory();
		policies = (ArrayList<Policy>) referenceRepository.getPolicies();
		if ((int) (eventBuffer.getSize() / 1000) == 0) {
		} else if ((int) (eventBuffer.getSize() / 1000) < 10) {
			i = 1;
		} else if ((int) (eventBuffer.getSize() / 1000) < 200) {
			i = 5;
		} else {
			i = 10;
		}
		executor = Executors.newFixedThreadPool(i);
	}

	public void run() {
		while (run) {

			if (eventBuffer.getSize() >= i) {
				executor.execute(new Wrapper(policies, ds));
			}
		}
	}

}
